package com.yungee.task;

import com.obs.services.ObsClient;
import com.obs.services.exception.ObsException;
import com.obs.services.model.*;
import com.yungee.zhaosheng.entity.StudentCard;
import com.yungee.zhaosheng.service.impl.StudentCardServiceImpl;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.IOException;
import java.util.Objects;

@Slf4j
@Data
public class UploadFileHaweiTask implements Runnable {


    private Long studendCardId;
    /**
     * 文件上传地址
     */
    private String filePosition;
    /**
     * 上传文件
     */
    private File file;

    /**
     *
     */
    private StudentCardServiceImpl studentCardService;

    /**
     * When an object implementing interface <code>Runnable</code> is used
     * to create a thread, starting the thread causes the object's
     * <code>run</code> method to be called in that separately executing
     * thread.
     * <p>
     * The general contract of the method <code>run</code> is that it may
     * take any action whatsoever.
     *
     * @see Thread#run()
     */
    @Override
    public void run() {
        String endpoint = "obs.cn-north-1.myhuaweicloud.com";
        String accessKeyId ="SKF1HBTA8PHP9HP4NGKK";
        String accessKeySecret = "7BTvYaJi41Omn8Lw8cM1NHWuWcHMR5ynNVg7G1Q5";
        String bucketName = "hongyu";
        log.info("filePosition={}",filePosition);
        // 创建ObsClient实例
        ObsClient obsClient = new ObsClient(accessKeyId, accessKeySecret, endpoint);
        try {
            //容器不存在，就创建
            if (!obsClient.headBucket(bucketName)) {
                CreateBucketRequest createBucketRequest = new CreateBucketRequest(bucketName);
                createBucketRequest.setAcl(AccessControlList.REST_CANNED_PUBLIC_READ_WRITE);
                ObsBucket bucket = obsClient.createBucket(createBucketRequest);
                log.info(bucket.toString());
            }
            PutObjectRequest request = new PutObjectRequest(bucketName, filePosition);
            request.setFile(file);
            // 每上传1MB数据反馈上传进度
            request.setProgressInterval(1024 * 500L);
            //上传文件
            PutObjectResult result = obsClient.putObject(request);
            if (Objects.nonNull(result) && StringUtils.isNotEmpty(result.getObjectKey())) {
                log.info("==========>OSS文件上传成功,status={},objectKey={}", result.getStatusCode(),result.getObjectKey());
                // 上传到OSS
                StudentCard studentCard = new StudentCard();
                studentCard.setId(studendCardId);
                studentCard.setFileResourceObsUrl(result.getObjectKey());
                studentCardService.updateById(studentCard);
            }else {
                log.info("==========>OSS文件上传失败");
                StudentCard studentCard = new StudentCard();
                studentCard.setId(studendCardId);
                studentCard.setFileResourceObsUrl("-");
                studentCardService.updateById(studentCard);
            }
            //设置权限 这里是公开读
        } catch (ObsException oe) {
            log.error("上传华为云oss报错:{}", oe);
        } finally {
            //关闭
            try {
                obsClient.close();
            } catch (IOException e) {
                log.error("关闭通道失败");
            }
        }
    }
}
